package com.milhouz.bomberrokon.util;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.stickycoding.Rokon.Rokon;

public class BomberXmlReader {

	public static final String ASSETS_IMG_FOLDER = "img";
	public static final String ASSETS_BOMBER_FOLDER = "Bombers";
	public static final String THUMB_FILENAME = "thumb.png";
	public static final String XML_MAP_FILENAME = "bomber.xml";

	private String bomberCode;
	private String tileSize;

	public BomberXmlReader(String bomberCode, String tileSize) {
		this.bomberCode = bomberCode;
		this.tileSize = tileSize;
	}

	public void parse() {
		InputStream mapInputStream = null;
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			xr.setContentHandler(new DefaultHandler() {

				@Override
				public void startDocument() throws SAXException {
					super.startDocument();
				}

				@Override
				public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
					super.startElement(uri, localName, name, atts);
					if (localName.equalsIgnoreCase("map")) {

					}
				}

				@Override
				public void endElement(String uri, String localName, String name) throws SAXException {
					super.endElement(uri, localName, name);
				}

				@Override
				public void characters(char[] ch, int start, int length) throws SAXException {
					super.characters(ch, start, length);
				}

			});
			mapInputStream = Rokon.getRokon().getActivity().getAssets().open(ASSETS_BOMBER_FOLDER + "/" + bomberCode + "/" + XML_MAP_FILENAME);
			xr.parse(new InputSource(mapInputStream));
		} catch (Exception e) {
			Log.e("bomberman", "SAX parsing failed", e);
		} finally {
			try {
				if (mapInputStream != null) {
					mapInputStream.close();
				}
			} catch (IOException e) {
				Log.e("bomberman", "Impossible to close XML bomber input stream : " + bomberCode, e);
			}
		}
	}
}
